package com.h1.staffmanage.service.impl;

import com.h1.staffmanage.entity.RespBean;
import com.h1.staffmanage.entity.TDepartment;
import com.h1.staffmanage.mapper.TDepartmentMapper;
import com.h1.staffmanage.service.TDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mengfanyu
 * @since 2022-06-23
 */
@Service
public class TDepartmentServiceImpl extends ServiceImpl<TDepartmentMapper, TDepartment> implements TDepartmentService {
    @Autowired
    private TDepartmentMapper departmentMapper;

    /**
     * 获取所有部门
     *
     * @return
     */
    @Override
    public List<TDepartment> getAllDepartments() {
        return departmentMapper.getAllDepartments(-1);
    }

    /**
     * 添加部门
     *
     * @param dep
     * @return
     */
    @Override
    public RespBean addDep(TDepartment dep) {
        dep.setEnabled(true);
        departmentMapper.addDep(dep);
        if (dep.getResult() == 1) {
            return RespBean.success("添加成功!", dep);
        }
        return RespBean.error("添加失败!");
    }

    /**
     * 删除部门
     *
     * @param id
     * @return
     */
    @Override
    public RespBean deleteDep(Integer id) {
        TDepartment dep = new TDepartment();
        dep.setId(id);
        departmentMapper.deleteDep(dep);
        if (dep.getResult() == -2) {
            return RespBean.error("该部门下还有子部门，删除失败!");
        }
        if (dep.getResult() == -1) {
            return RespBean.error("该部门下还有员工，删除失败!");
        }
        if (dep.getResult() == 1) {
            return RespBean.success("删除成功!");
        }
        return RespBean.error("删除失败!");
    }
}
